//
//  YHYOwnerViewController.m
//  Home
//
//  Created by 杨泓晔 on 16/4/18.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "YHYDesigneViewsViewController.h"
#import "YHYDesignecell.h"
#import "WSHeaderActView.h"
#import "WSCustomPickerView.h"
#import "DCPaymentView.h"
//#import "WSShopInfoNextView.h"
@interface YHYDesigneViewsViewController ()<UITableViewDataSource,UITableViewDelegate,WSHeaderActViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,WSShopInfoNextViewDelegate,WSCustomPickerViewDelegate>
@property (nonatomic,strong)  UITableView *myTableView;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) UIImage *headImage;
@property (nonatomic, strong) WSHeaderActView *headerAV;
@property (nonatomic, strong) WSCustomPickerView *customPicker;
@property (nonatomic, strong) NSMutableArray *contentAry;
@property (nonatomic,strong)  UITextField *nameTextField;


@end

@implementation YHYDesigneViewsViewController
- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBarHidden = NO;
    self.title = @"基本信息";
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

    self.view.backgroundColor = LV;
    self.array = [NSArray arrayWithObjects:@"头像", @"性别", @"姓名", @"微信号", @"常用邮箱", nil];
    self.contentAry = [NSMutableArray arrayWithObjects:@"必填", @"选填", @"选填", @"选填", nil];
    
    self.myTableView = [UITableView new];
    
    [self.view addSubview:self.myTableView];
    self.myTableView.rowHeight = ROWHEIGHT;
    // self.myTableView.sectionFooterHeight = 10;
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(self.view);
        make.height.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view);
        
    }];
    
    
    _myTableView.scrollEnabled = NO;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.backgroundColor = LV;
    //[self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerClass:[YHYDesignecell class] forCellReuseIdentifier:@"YHYOwnercell"];
    [[UITableViewHeaderFooterView appearance] setTintColor:LV];
    [_myTableView reloadData];
    
}
// 选中照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    _headImage = [info objectForKey:UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:NULL];
    [self.myTableView reloadData];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }
    if (section == 1) {
        return 2;
    }
    else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellstr = @"YHYOwnercell";
    YHYDesignecell *cell = [tableView dequeueReusableCellWithIdentifier:cellstr];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

   // if (cell == nil) {
    //    cell = [[YHYOwnercell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellstr];
             cell.string = _array[indexPath.row + indexPath.section * 1];
        
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.image = _headImage;
            //cell.image = _
            cell.type = 2;

        }
        if (indexPath.row == 1) {
            cell.type = 3;
            
            
        }
        if (indexPath.row == 2) {
            cell.type = 1;
            cell.content = self.contentAry[indexPath.row];
            
        }
          }
    if (indexPath.section == 1) {
       cell.string = _array[indexPath.row+ 1 + indexPath.section * 2];
        if (indexPath.row == 0) {
            cell.type = 1;
            cell.content = self.contentAry[indexPath.row];
        }
        if (indexPath.row == 1) {
            cell.type = 1;
            cell.content = self.contentAry[indexPath.row];
        }
   // }
}

   
    return cell;

    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0 && indexPath.section == 0) {
        
        [self.headerAV show];
    }
//    else if (indexPath.row == 1 && indexPath.section == 1) {
//        
//        [self.customPicker showPicker];
//    }
    if (indexPath.row == 1 && indexPath.section == 0) {
        
    }
    if (indexPath.section == 0 && indexPath.row == 2) {
        DCPaymentView *payAlert = [DCPaymentView new];
        payAlert.title = [NSString stringWithFormat:@"请输入%@",_array[indexPath.row]];
        [payAlert show];
        payAlert.completeHandle = ^(NSString *inputPwd) {
        
           
            self.contentAry[indexPath.row] = inputPwd;
            
            [self.myTableView reloadData];
           
        };
    
       
    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        DCPaymentView *payAlert = [DCPaymentView new];
      NSString *str = [_array objectAtIndex:indexPath.row+ 2 + indexPath.section];
        payAlert.title = [NSString stringWithFormat:@"请输入%@",str];
        [payAlert show];
        payAlert.completeHandle = ^(NSString *inputPwd) {
            
            self.contentAry[indexPath.row] = inputPwd;
            
            [self.myTableView reloadData];

        };
    }
    
    if (indexPath.section == 1 && indexPath.row == 1) {
        DCPaymentView *payAlert = [DCPaymentView new];
        NSString *str = [_array objectAtIndex:indexPath.row+ 2 + indexPath.section];
        payAlert.title = [NSString stringWithFormat:@"请输入%@",str];
        [payAlert show];
        payAlert.completeHandle = ^(NSString *inputPwd) {
            
            self.contentAry[indexPath.row] = inputPwd;
            
            [self.myTableView reloadData];
            
        };

    }
    
//    else{
//        DCPaymentView *payAlert = [DCPaymentView new];
//        payAlert.title = [NSString stringWithFormat:@"请输入%@",_array[indexPath.row]];
//        [payAlert show];
//        payAlert.completeHandle = ^(NSString *inputPwd) {
//            NSLog(@"%ld", indexPath.row);
//            self.contentAry[indexPath.row + (indexPath.section * 2)] = inputPwd;
//            [self.myTableView reloadData];
//
//        
//        };
//    }

}
#pragma mark - 选择器
- (WSCustomPickerView *)customPicker {
    
    if (!_customPicker) {
        self.customPicker = [[WSCustomPickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.customPicker.titleStr = @"商家地址";
        self.customPicker.delegate = self;
        [self.view addSubview:self.customPicker];
    }
    
    return _customPicker;
}


#pragma mark - 头像点击View
- (WSHeaderActView *)headerAV {
    
    if (!_headerAV) {
        self.headerAV = [[WSHeaderActView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.headerAV.delegate = self;
        [self.view addSubview:_headerAV];
    }
    
    return _headerAV;
}
#pragma mark - headerAV 点击事件代理
- (void)presentImagePicker:(OPENIMAGE)img {
    
    if (img == 0) {
        [self selectCamera];
    } else if (img == 1) {
        [self selectPics];
    } else {
        
    }
}
- (void)selectCamera {
    
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
    picker.delegate = self;
    picker.allowsEditing = YES;//设置可编辑
    picker.sourceType = sourceType;
    [self presentViewController:picker animated:YES completion:nil];//进入照相界面
}

- (void)selectPics {
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    [self presentViewController:imagePicker animated:YES completion:nil];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section == 1) {
        
        return 8 * AUTO_HEIGHT;
    }
    
    if (section == 2) {
        return 150 * AUTO_HEIGHT;
    }
    if (section == 3) {
        return 300 * AUTO_HEIGHT;
    }
    
    return 8 * AUTO_HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 1;
}

-(void)Back:(UIButton *) goback
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 2) {
        
    
   _InfoNextView = [[WSShopInfoNextView alloc] initWithFrame:CGRectMake(0, 1000* AUTO_HEIGHT, SCREEN_WIDTH, 40 * AUTO_HEIGHT)];
    _InfoNextView.delegate = self;
    
    return _InfoNextView;
    }
    return nil;
    
}


- (void)nextAction{
    if (_mun == 1) {
   
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeNameNotification" object:self userInfo:@{@"select":@"1"}];
        
    }
    if (_mun == 2) {
        
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeNameNotification" object:self userInfo:@{@"select":@"2"}];
    }

}




@end
